package com.xxkj.orcfx.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxkj.orcfx.web.BaiduWordEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ExcelCustomerInfoReportVo implements Serializable {

    @ExcelProperty(value ="序号", index = 0)
    private String serialNo;

    @ExcelProperty(value ="借款人名称", index = 1)
    private String customerName;

    @ExcelProperty(value ="借款人身份证号码", index = 2)
    private String certId;

    @ExcelProperty(value ="信用卡申请表", index = 3)
    private String apply;

    @ExcelProperty(value ="信用卡激活", index = 4)
    private String active;

    @ExcelProperty(value ="申请确认函", index = 5)
    private String confirm;

    @ExcelProperty(value ="民族", index = 6)
    private String nation;

    @ExcelProperty(value ="户籍所在地", index = 7)
    private String address;

    @ExcelProperty(value ="身份证有效期", index = 8)
    private String validDate;

    @ExcelProperty(value ="备注", index = 9)
    private String remark;

    @ExcelProperty(value ="历史被保全情况", index = 10)
    private String historyInfo;

    @ExcelProperty(value ="客户编号", index = 11)
    private String imgSerialNo;


    public static List<ExcelCustomerInfoReportVo> adapterMap(Map<String, Map<String, String>> customerMap){
        List<ExcelCustomerInfoReportVo> list = new ArrayList<>();
        if(MapUtils.isEmpty(customerMap)) return list;
        int serialno = 1;
        for(String reportKey: customerMap.keySet()){
            Map<String, String> reportMap = customerMap.get(reportKey);
            if(reportMap == null || reportMap.isEmpty()) continue;
            list.add(adapter(serialno, reportKey, reportMap));
            serialno++;
        }
        return list;
    }

    public static ExcelCustomerInfoReportVo adapter(int serialNo, String reportKey, Map<String, String> reportMap){
        ExcelCustomerInfoReportVo vo = new ExcelCustomerInfoReportVo();
        vo.setSerialNo(serialNo + "");
        String customerName = null;
        String seqNo = "";
        String[] parentNameArray = reportKey.split("_");
        if(parentNameArray.length == 3) {
            customerName = parentNameArray[0];
            seqNo = parentNameArray[1];
        }
        String idcardInfo = reportMap.get("idcardInfo");
        String remark = "";
        if(StringUtils.isNotEmpty(idcardInfo)) {
            JSONObject idcardObj = JSON.parseObject(idcardInfo);
            vo.setCertId(idcardObj.getString(BaiduWordEnum.ident.getToWord()));
            vo.setNation(idcardObj.getString(BaiduWordEnum.nation.getToWord()));
            vo.setAddress(idcardObj.getString(BaiduWordEnum.address.getToWord()));
            vo.setValidDate(idcardObj.getString(BaiduWordEnum.validBegin.getToWord()) + "/" + idcardObj.getString(BaiduWordEnum.validEnd.getToWord()));
            String idcardName = idcardObj.getString(BaiduWordEnum.name.getToWord());
            if(customerName == null)  customerName = idcardName;
            if(idcardName != null && !idcardName.equals(customerName)) {
                remark = "ocr识别姓名[" + idcardName + "]";
            }
        }
        vo.setCustomerName(customerName);

        vo.setApply("1".equals(reportMap.get("apply")) ? "1" : "");
        vo.setActive("1".equals(reportMap.get("active")) ? "1" : "");
        vo.setConfirm("1".equals(reportMap.get("confirm")) ? "1" : "");
        vo.setRemark(remark);
        vo.setHistoryInfo(reportMap.get("historyInfo"));
        vo.setImgSerialNo(seqNo);
        return vo;
    }

    public static String format(Double d){
        if(d==null)
            return "0.0";
        return d + "";
    }
}
